<!DOCTYPE html>
<html lang="en">
<!--头部信息-->
<head>
    <meta charset="utf-8">
    <title>添加用户</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../../../css/public.css" media="all">

    <script src="../../../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>

    <style>
        body {
            background-color: #ffffff;
        }
    </style>

</head>
<!--死薄添加用户-->
<body>

<div class="layui-form layuimini-form">

    <!--姓名-->
    <div class="layui-form-item">
        <label class="layui-form-label required">姓名</label>
        <div class="layui-input-block" style="width: 80%">
            <input type="text" name="username" id="username" lay-verify="required" lay-reqtext="用户名不能为空"
                   placeholder="请输入用户名"
                   value=""
                   class="layui-input">
        </div>
    </div>
    <!--性别-->
    <div class="layui-form-item">
        <label class="layui-form-label required">性别</label>
        <div class="layui-input-block">
            <input type="radio" name="sex" value="男" title="男" checked="">
            <input type="radio" name="sex" value="女" title="女">
        </div>
    </div>
    <!--类别-->
    <div class="layui-form-item">
        <label class="layui-form-label required">类别</label>
        <div class="layui-input-block" style="width: 80%">
            <select name="type" id="type" lay-filter="aihao">
                <option value="" disabled="disabled">请选择</option>
            </select>
        </div>
    </div>
    <!--地区-->
    <div class="layui-form-item ">
        <label class="layui-form-label required">地区</label>
        <div class="layui-input-block" style="width: 80%">
            <select name="area" id="area" lay-filter="aihao">
                <option value="" disabled="disabled">请选择</option>
            </select>
        </div>
    </div>
    <!--寿命-->
    <div class="layui-form-item">
        <label class="layui-form-label required">寿命</label>
        <div class="layui-input-block" style="padding-left: 8px;padding-top: 17px;width: 79.7%">
            <div id="age" class="demo-slider"></div>
        </div>
    </div>
    <!--备注-->
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">备注</label>
        <div class="layui-input-block" style="width: 80%">
            <textarea placeholder="请输入内容" class="layui-textarea" name="desc" id="desc" style="height: 160px"></textarea>
        </div>
    </div>
    <!--确认按钮-->
    <div class="layui-form-item">
        <div class="layui-input-block ">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认</button>
            <button class="layui-btn " id="restBtn">取消</button>
        </div>
    </div>
</div>

<script>
    layui.use(['form', 'slider'], function () {
        var form = layui.form,
            layer = layui.layer,
            slider = layui.slider,
            $ = layui.$,
            sliderInsValue = 0;

        getTypeList();
        getAreaList();

        /*渲染类别下拉列表框*/
        function getTypeList() {
            $.ajax({
                type: "GET",
                url: "/getTypeList",
                dataType: "json",
                success: function (data) {
                    $.each(data, function (index, item) {
                        $('#type').append(new Option(item.typeName, item.typeId));
                        $('#type').val("1");
                    });
                    form.render("select");
                }
            });
        }

        /*渲染地区下拉列表框*/
        function getAreaList() {
            $.ajax({
                type: "GET",
                url: "/getAreaList",
                dataType: "json",
                success: function (data) {
                    $.each(data, function (index, item) {
                        $('#area').append(new Option(item.districtName, item.id));
                        $('#area').val("2");
                    });
                    form.render("select");
                }
            });
        }

        /*滑块初始化*/
        slider.render({
            elem: "#age",
            theme: '#5FB878',
            min: 0,
            max: 150,
            setTips: function (value) {
                sliderInsValue = value;
                return value + '岁';
            },
            change: function (va) {
                // sliderInsValue = va;
            }
        });


        /*保存事件*/
        form.on('submit(saveBtn)', function (data) {
            var lifeinfo = {
                lifeName: $('#username').val().trim(),
                lifeSex: $('input[name="sex"]:checked').val(),
                lifeCountAge: sliderInsValue,
                lifeType: $('#type').val(),
                lifeArea: $('#area').val(),
                lifeDesc: $('#desc').val().trim()
            };
            layer.confirm("确认添加吗？",{icon: 3}, function (index) {
                $.ajax({
                    type: 'POST',
                    dataType: 'text',
                    url: '/LivebookAdd',
                    data: {lifeInfo: JSON.stringify(lifeinfo)},
                    error: function () {
                        layer.alert("系统错误！");
                    },
                    success: function (result) {
                        if (result === 'success') {
                            layer.alert('添加成功！',
                                {
                                    title: '成功',
                                    skin: 'layui-layer-molv',
                                    btn: ['返回'],
                                    anim: 1,
                                    icon: 6,
                                    yes: function () {
                                        // setTimeout(function () {
                                        //     window.parent.location.reload();//修改成功后刷新父界面
                                        // }, 1000);
                                        parent.layui.table.reload('testId', {
                                            page: {
                                                curr: 1
                                            },
                                        });
                                        layer.close(index);
                                        parent.layer.close(parent.layer.getFrameIndex(window.name));
                                    }
                                });
                        } else {
                            layer.alert('添加失败，请重试？',
                                {
                                    title: '错误',
                                    skin: 'layui-layer-molv',
                                    btn: ['重试', '退出'],
                                    anim: 1,
                                    icon: 5,
                                    yes: function (index) {
                                        layer.close(index);
                                    },
                                    btn2: function () {
                                        layer.close(index);
                                        parent.layer.close(parent.layer.getFrameIndex(window.name));
                                    }
                                });
                        }
                    }
                });
            });

        });

        /*取消事件*/
        $("#restBtn").click(function () {
            parent.layer.close(parent.layer.getFrameIndex(window.name));
        })
    });
</script>
</body>
</html>